
#pragma once
#include "ClientStdafx.h"
#include "UndoManager.h"
#include "ClientRoot.h"
namespace Apo
{


	//----------------------------------------------------------------------------------
	UndoManager::UndoManager()
	{
		m_CurrentIndex = -1;
		m_List.clear();
	}
	//----------------------------------------------------------------------------------  
	UndoManager::~UndoManager()
	{
		ClearList(0,m_List.size()/*m_List.begin(), m_List.end()*/);
	}
	//----------------------------------------------------------------------------------  
	void UndoManager::ClearList(int itbegin, int itend)
	{
// 		UndoList::iterator it = itbegin;
// 		while(it != itend)
// 		{
// 			if(itbegin->m_SubData.size() > 0)
// 				ClearListItem(itbegin->m_SubData);
// 			ClearCustomPropertyData(itbegin->m_CustomProperties); 
// 			it++;
// 		}
// 		m_List.erase(itbegin, itend);
	}
	//----------------------------------------------------------------------------------
	void UndoManager::ClearListItem(UndoList &item)
	{
// 		UndoList::iterator it = item.begin();
// 		while(it != item.end())
// 		{
// 			if(it->m_SubData.size() > 0)
// 				ClearListItem(it->m_SubData);
// 			ClearCustomPropertyData(it->m_CustomProperties);
// 			it++;
// 		}
// 		item.clear();
	}
	//----------------------------------------------------------------------------------
	void UndoManager::ClearCustomPropertyData(CustomPropertyList &customlist)
	{
		for(unsigned int i = 0;i < customlist.size();i++)
		{
			ClientRoot::getInstance().RecurseDeleteCustomProperties(customlist[i]);
		}
		customlist.clear();
	}
	//----------------------------------------------------------------------------------
	void UndoManager::AddUndo(OGITORUNDOSTRUCT undodata)
	{
		++m_CurrentIndex;
		if(m_CurrentIndex < (int)m_List.size())
			ClearList(0 + m_CurrentIndex, m_List.size());

		m_List.push_back(undodata);
	}
	//----------------------------------------------------------------------------------
	void UndoManager::Undo()
	{
		if(!CanUndo()) return;

		ApplyUndo(m_List[m_CurrentIndex], true);

		--m_CurrentIndex;
	}
	//----------------------------------------------------------------------------------
	void UndoManager::ApplyUndo(OGITORUNDOSTRUCT &undostruct, bool setselected)
	{
// 		CBaseEditor *object;
// 		Apo::String namechangestore = "";
// 		if(undostruct.m_ObjectProperties["newname"] != "")
// 		{
// 			object = ClientRoot::getInstance().FindObject(undostruct.m_ObjectProperties["newname"]);
// 			namechangestore = undostruct.m_ObjectProperties["name"];
// 		}
// 		else
// 			object = ClientRoot::getInstance().FindObject(undostruct.m_ObjectProperties["name"]);            
// 
// 		switch(undostruct.m_Operation) 
// 		{
// 		case UNDO_CREATE:
// 			{
// 				if(object)
// 				{    
// 					ClientRoot::getInstance().DestroyEditorObject(object, true);
// 					object = 0;
// 				}
// 				break;
// 			}
// 
// 		case UNDO_DELETE: 
// 			{
// 				if(undostruct.m_ObjectProperties["objecttype"] != "Multi Selection")
// 					object = ClientRoot::getInstance().CreateEditorObject(0,undostruct.m_ObjectProperties["objecttype"],undostruct.m_ObjectProperties,true,true);
// 				break;
// 			}
// 
// 		default:
// 			{
// 				Apo::String seltarget = undostruct.m_ObjectProperties["undoselectiontarget"];
// 				if(object)
// 				{
// 					object = object->Restore(undostruct.m_ObjectProperties);
// 					if(namechangestore != "")
// 						undostruct.m_ObjectProperties["newname"] = namechangestore;
// 					object->RestoreCustomProperties(undostruct.m_CustomProperties);
// 				}
// 				if(seltarget != "")
// 					undostruct.m_ObjectProperties["undoselectiontarget"] = seltarget; 
// 				break;
// 			}
// 		}
// 
// 		UndoList::iterator it = undostruct.m_SubData.begin();
// 
// 		while(it != undostruct.m_SubData.end())
// 		{
// 			ApplyUndo(*it, false);
// 			it++;
// 		}
// 
// 		if(object && object->GetObjectTypeString() == "Multi Selection")
// 			object->Restore(undostruct.m_ObjectProperties);
// 
// 		if(setselected)
// 		{
// 			ClientRoot::getInstance().GetRootEditor()->UpdateTreeView();
// 
// 			if(undostruct.m_ObjectProperties["undoselectiontarget"] != "")
// 			{
// 				object = ClientRoot::getInstance().FindObject(undostruct.m_ObjectProperties["undoselectiontarget"]); 
// 			}
// 
// 			if(object)
// 			{
// 				if(object && object->GetObjectTypeString() == "Multi Selection")
// 					object->SetSelected(true);
// 				else                     
// 					object->UpdateTreeView();
// 			}
// 		}
	}
	//----------------------------------------------------------------------------------
	void UndoManager::Redo()
	{
		if(!CanRedo()) return;
		++m_CurrentIndex;
		ApplyRedo(m_List[m_CurrentIndex], true);
	}
	//----------------------------------------------------------------------------------
	void UndoManager::ApplyRedo(OGITORUNDOSTRUCT &undostruct, bool setselected)
	{
// 		CBaseEditor *object;
// 		Apo::String namechangestore = "";
// 		if(undostruct.m_ObjectProperties["newname"] != "")
// 		{
// 			object = ClientRoot::getInstance().FindObject(undostruct.m_ObjectProperties["newname"]);
// 			namechangestore = undostruct.m_ObjectProperties["name"];
// 		}
// 		else
// 			object = ClientRoot::getInstance().FindObject(undostruct.m_ObjectProperties["name"]);            
// 
// 		switch(undostruct.m_Operation) 
// 		{
// 		case UNDO_CREATE:
// 			{
// 				object = ClientRoot::getInstance().CreateEditorObject(0,undostruct.m_ObjectProperties["objecttype"],undostruct.m_ObjectProperties,true,true);
// 				break;
// 			}
// 
// 		case UNDO_DELETE: 
// 			{
// 				if(object && (undostruct.m_ObjectProperties["objecttype"] != "Multi Selection"))
// 				{    
// 					ClientRoot::getInstance().DestroyEditorObject(object, true);
// 					object = 0;
// 				}
// 				break;
// 			}
// 
// 		default:
// 			{
// 				Apo::String seltarget = undostruct.m_ObjectProperties["undoselectiontarget"];
// 				if(object)
// 				{
// 					object = object->Restore(undostruct.m_ObjectProperties);
// 					if(namechangestore != "")
// 						undostruct.m_ObjectProperties["newname"] = namechangestore;
// 					object->RestoreCustomProperties(undostruct.m_CustomProperties);
// 				}
// 				if(seltarget != "")
// 					undostruct.m_ObjectProperties["undoselectiontarget"] = seltarget; 
// 				break;
// 			}
// 		}
// 
// 		UndoList::iterator it = undostruct.m_SubData.begin();
// 
// 		while(it != undostruct.m_SubData.end())
// 		{
// 			ApplyRedo(*it, false);
// 			it++;
// 		}
// 
// 		if(object && object->GetObjectTypeString() == "Multi Selection")
// 		{
// 			if(undostruct.m_Operation != UNDO_DELETE)
// 				object->Restore(undostruct.m_ObjectProperties);
// 			else
// 			{        
// 				Apo::NameValuePairList theList;         
// 				theList.clear();
// 				object->Restore(theList);
// 			}
// 		}
// 
// 		if(setselected)
// 		{
// 			ClientRoot::getInstance().GetRootEditor()->UpdateTreeView();
// 
// 			if(undostruct.m_ObjectProperties["undoselectiontarget"] != "")
// 			{
// 				object = ClientRoot::getInstance().FindObject(undostruct.m_ObjectProperties["undoselectiontarget"]); 
// 			}
// 
// 			if(object)
// 			{
// 				if(object && object->GetObjectTypeString() == "Multi Selection")
// 				{
// 					if(undostruct.m_Operation != UNDO_DELETE)
// 						object->SetSelected(true);
// 					else
// 						object->SetSelected(false);
// 				}
// 				else                     
// 					object->UpdateTreeView();
// 			}
// 		}
	}
	//----------------------------------------------------------------------------------

};
